using System;
using System.Collections.Generic;

namespace Matrix.Configuration.DTOs
{
    /// <summary>
    /// 命名空间配置DTO
    /// </summary>
    public class NamespaceConfigurationDto
    {
        /// <summary>
        /// 配置名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 配置值
        /// </summary>
        public string Value { get; set; } = string.Empty;

        /// <summary>
        /// 命名空间
        /// </summary>
        public string Namespace { get; set; } = "default";

        /// <summary>
        /// 配置类型
        /// </summary>
        public string Type { get; set; } = string.Empty;

        /// <summary>
        /// 是否为默认值
        /// </summary>
        public bool IsDefault { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>
        public DateTime LastModified { get; set; }

        /// <summary>
        /// 最后修改者
        /// </summary>
        public string LastModifier { get; set; } = string.Empty;

        /// <summary>
        /// 描述
        /// </summary>
        public string? Description { get; set; }

        /// <summary>
        /// 配置版本
        /// </summary>
        public long Version { get; set; }
    }

    /// <summary>
    /// 配置历史记录DTO
    /// </summary>
    public class ConfigurationHistoryDto
    {
        /// <summary>
        /// 历史记录ID
        /// </summary>
        public Guid Id { get; set; }

        /// <summary>
        /// 配置键
        /// </summary>
        public string Key { get; set; } = string.Empty;

        /// <summary>
        /// 配置命名空间
        /// </summary>
        public string Namespace { get; set; } = string.Empty;

        /// <summary>
        /// 旧值
        /// </summary>
        public string? OldValue { get; set; }

        /// <summary>
        /// 新值
        /// </summary>
        public string? NewValue { get; set; }

        /// <summary>
        /// 变更时间
        /// </summary>
        public DateTime ChangeTime { get; set; }

        /// <summary>
        /// 变更者
        /// </summary>
        public string ChangedBy { get; set; } = string.Empty;

        /// <summary>
        /// 变更类型
        /// </summary>
        public ConfigurationChangeType ChangeType { get; set; }

        /// <summary>
        /// 变更原因
        /// </summary>
        public string? Reason { get; set; }

        /// <summary>
        /// 租户ID
        /// </summary>
        public Guid? TenantId { get; set; }
    }

    /// <summary>
    /// 配置变更类型枚举
    /// </summary>
    public enum ConfigurationChangeType
    {
        /// <summary>
        /// 创建
        /// </summary>
        Created = 1,

        /// <summary>
        /// 更新
        /// </summary>
        Updated = 2,

        /// <summary>
        /// 删除
        /// </summary>
        Deleted = 3,

        /// <summary>
        /// 还原
        /// </summary>
        Restored = 4
    }

    // === 事件调试相关DTO（从原有接口迁移）===

    /// <summary>
    /// 事件调试DTO
    /// </summary>
    public class EventDebugDto
    {
        public Guid EventId { get; set; }
        public string EventType { get; set; } = string.Empty;
        public string Key { get; set; } = string.Empty;
        public string Namespace { get; set; } = string.Empty;
        public string Status { get; set; } = string.Empty;
        public DateTime EventTime { get; set; }
        public int Duration { get; set; }
        public Guid? TenantId { get; set; }
        public string TraceId { get; set; } = string.Empty;
        public string? OldValue { get; set; }
        public string? NewValue { get; set; }
        public string ValueType { get; set; } = string.Empty;
        public string Importance { get; set; } = string.Empty;
        public string ChangeSource { get; set; } = string.Empty;
        public string SourceService { get; set; } = string.Empty;
        public bool IsSensitive { get; set; }
        public string? ErrorMessage { get; set; }
        public IReadOnlyList<EventStepDebugDto> Steps { get; set; } = new List<EventStepDebugDto>();
    }

    /// <summary>
    /// 事件步骤调试DTO
    /// </summary>
    public class EventStepDebugDto
    {
        public string StepName { get; set; } = string.Empty;
        public int Duration { get; set; }
        public bool Success { get; set; }
        public string? ErrorMessage { get; set; }
    }

    /// <summary>
    /// 事件统计DTO
    /// </summary>
    public class EventStatisticsDto
    {
        public long TotalEvents { get; set; }
        public long TodayEvents { get; set; }
        public long WeekEvents { get; set; }
        public long MonthEvents { get; set; }
        public IReadOnlyDictionary<string, long> EventsByImportance { get; set; } = new Dictionary<string, long>();
        public IReadOnlyDictionary<string, long> EventsBySource { get; set; } = new Dictionary<string, long>();
        public IReadOnlyDictionary<string, long> EventsByCategory { get; set; } = new Dictionary<string, long>();
        public double SuccessRate { get; set; }
        public double AverageProcessingTime { get; set; }
        public long CurrentActiveEvents { get; set; }
        public long LocalEventCount { get; set; }
        public long DistributedEventCount { get; set; }
        public DateTime LastUpdated { get; set; }
    }

    /// <summary>
    /// 事件重放结果DTO
    /// </summary>
    public class EventReplayResultDto
    {
        public string Message { get; set; } = string.Empty;
        public Guid EventId { get; set; }
        public DateTime ReplayTime { get; set; }
        public bool Success { get; set; }
        public string? ErrorMessage { get; set; }
    }

    /// <summary>
    /// 事件性能统计DTO
    /// </summary>
    public class EventPerformanceStatsDto
    {
        public string EventType { get; set; } = string.Empty;
        public long TotalProcessed { get; set; }
        public long SuccessCount { get; set; }
        public long FailureCount { get; set; }
        public double SuccessRate { get; set; }
        public double AverageProcessingTime { get; set; }
        public double MinProcessingTime { get; set; }
        public double MaxProcessingTime { get; set; }
        public double P95ProcessingTime { get; set; }
        public double P99ProcessingTime { get; set; }
        public TimeRangeDto TimeRange { get; set; } = new();
    }

    /// <summary>
    /// 时间范围DTO
    /// </summary>
    public class TimeRangeDto
    {
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
    }

    // === 服务定义相关DTO（从原有接口迁移）===

    /// <summary>
    /// 定义服务输入
    /// </summary>
    public class DefineServiceInput
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; } = string.Empty;

        /// <summary>
        /// 服务版本
        /// </summary>
        public string Version { get; set; } = "1.0.0";

        /// <summary>
        /// 服务描述
        /// </summary>
        public string? Description { get; set; }

        /// <summary>
        /// 配置定义
        /// </summary>
        public System.Collections.ObjectModel.Collection<ServiceConfigurationInput> Configurations { get; } = new();

        /// <summary>
        /// 依赖的服务
        /// </summary>
        public System.Collections.ObjectModel.Collection<string> Dependencies { get; } = new();
    }

    /// <summary>
    /// 服务配置输入
    /// </summary>
    public class ServiceConfigurationInput
    {
        /// <summary>
        /// 配置名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 配置值
        /// </summary>
        public object Value { get; set; } = string.Empty;

        /// <summary>
        /// 配置类型
        /// </summary>
        public string Type { get; set; } = "String";

        /// <summary>
        /// 是否必需
        /// </summary>
        public bool IsRequired { get; set; }

        /// <summary>
        /// 默认值
        /// </summary>
        public object? DefaultValue { get; set; }

        /// <summary>
        /// 验证规则
        /// </summary>
        public string? ValidationRule { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string? Description { get; set; }
    }

    /// <summary>
    /// 服务定义DTO
    /// </summary>
    public class ServiceDefinitionDto
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; } = string.Empty;

        /// <summary>
        /// 服务版本
        /// </summary>
        public string Version { get; set; } = string.Empty;

        /// <summary>
        /// 服务描述
        /// </summary>
        public string? Description { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreationTime { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>
        public DateTime LastModificationTime { get; set; }

        /// <summary>
        /// 配置数量
        /// </summary>
        public int ConfigurationCount { get; set; }

        /// <summary>
        /// 依赖数量
        /// </summary>
        public int DependencyCount { get; set; }
    }

    /// <summary>
    /// 服务配置定义DTO
    /// </summary>
    public class ServiceConfigurationDefinitionDto
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; } = string.Empty;

        /// <summary>
        /// 服务版本
        /// </summary>
        public string Version { get; set; } = string.Empty;

        /// <summary>
        /// 配置列表
        /// </summary>
        public System.Collections.ObjectModel.Collection<ServiceConfigurationDto> Configurations { get; } = new();

        /// <summary>
        /// 依赖服务
        /// </summary>
        public System.Collections.ObjectModel.Collection<string> Dependencies { get; } = new();
    }

    /// <summary>
    /// 服务配置DTO
    /// </summary>
    public class ServiceConfigurationDto
    {
        /// <summary>
        /// 配置名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 配置值
        /// </summary>
        public string Value { get; set; } = string.Empty;

        /// <summary>
        /// 配置类型
        /// </summary>
        public string Type { get; set; } = string.Empty;

        /// <summary>
        /// 是否必需
        /// </summary>
        public bool IsRequired { get; set; }

        /// <summary>
        /// 默认值
        /// </summary>
        public string? DefaultValue { get; set; }

        /// <summary>
        /// 验证规则
        /// </summary>
        public string? ValidationRule { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string? Description { get; set; }
    }

    /// <summary>
    /// 更新服务输入
    /// </summary>
    public class UpdateServiceInput
    {
        /// <summary>
        /// 服务版本
        /// </summary>
        public string Version { get; set; } = string.Empty;

        /// <summary>
        /// 服务描述
        /// </summary>
        public string? Description { get; set; }

        /// <summary>
        /// 新增或更新的配置
        /// </summary>
        public List<ServiceConfigurationInput> Configurations { get; set; } = new();

        /// <summary>
        /// 要移除的配置名称
        /// </summary>
        public List<string> RemovedConfigurations { get; set; } = new();

        /// <summary>
        /// 新增的依赖
        /// </summary>
        public List<string> AddedDependencies { get; set; } = new();

        /// <summary>
        /// 要移除的依赖
        /// </summary>
        public List<string> RemovedDependencies { get; set; } = new();
    }

    /// <summary>
    /// 服务依赖关系DTO
    /// </summary>
    public class ServiceDependenciesDto
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; } = string.Empty;

        /// <summary>
        /// 直接依赖
        /// </summary>
        public System.Collections.ObjectModel.Collection<ServiceDependencyDto> DirectDependencies { get; } = new();

        /// <summary>
        /// 间接依赖
        /// </summary>
        public System.Collections.ObjectModel.Collection<ServiceDependencyDto> IndirectDependencies { get; } = new();

        /// <summary>
        /// 被依赖的服务
        /// </summary>
        public System.Collections.ObjectModel.Collection<ServiceDependencyDto> DependentServices { get; } = new();
    }

    /// <summary>
    /// 服务依赖DTO
    /// </summary>
    public class ServiceDependencyDto
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; } = string.Empty;

        /// <summary>
        /// 服务版本
        /// </summary>
        public string Version { get; set; } = string.Empty;

        /// <summary>
        /// 依赖类型
        /// </summary>
        public string DependencyType { get; set; } = string.Empty;

        /// <summary>
        /// 是否必需
        /// </summary>
        public bool IsRequired { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string? Description { get; set; }
    }
}